﻿Option Explicit On
Option Strict On

Imports System
Imports System.Collections


#Region "ConfigEventHandler class"

Public Class ConfigEventArgs
    Inherits EventArgs

#Region " Private Members "

    Private m_config As IConfig = Nothing

#End Region

#Region " Constructor "

    ''' <summary>
    ''' Constructor 
    ''' </summary>
    ''' <param name="config"></param>
    ''' <remarks></remarks>
    Public Sub New(config As IConfig)
        Me.m_config = config
    End Sub

#End Region

#Region " Public Properties "

    Public ReadOnly Property Config() As IConfig
        Get
            Return m_config
        End Get
    End Property

#End Region

End Class

#End Region

Public Class ConfigCollection
    Inherits ObjectModel.Collection(Of IConfig)

#Region " Private Members "

    Private m_owner As ConfigSourceBase = Nothing

#End Region

#Region " Constructors "

    ''' <summary>
    ''' Constructor 
    ''' </summary>
    ''' <param name="owner"></param>
    ''' <remarks></remarks>
    Public Sub New(owner As ConfigSourceBase)
        m_owner = owner
    End Sub

#End Region

#Region " Public Properties "

    Default Public Overloads ReadOnly Property Item(configName As String) As IConfig
        Get
            Dim result As IConfig = Nothing

            For Each config As IConfig In Me
                If config.Name = configName Then
                    result = config
                    Exit For
                End If
            Next

            Return result
        End Get
    End Property

#End Region

#Region " Public Methods "

    Public Overloads Function Add(name As String) As IConfig
        Dim result As ConfigBase = Nothing

        If Me(name) Is Nothing Then
            result = New ConfigBase(name, m_owner)
            Me.Add(result)
            OnConfigAdded(New ConfigEventArgs(result))
        Else
            Throw New ArgumentException("An IConfig of that name already exists")
        End If

        Return result
    End Function

#End Region

#Region " Public events "

    Public Event ConfigAdded As EventHandler(Of ConfigEventArgs)
    Public Event ConfigRemoved As EventHandler(Of ConfigEventArgs)

#End Region

#Region " Protected Methods "

    Protected Sub OnConfigAdded(e As ConfigEventArgs)
        RaiseEvent ConfigAdded(Me, e)
    End Sub

    Protected Sub OnConfigRemoved(e As ConfigEventArgs)
        RaiseEvent ConfigRemoved(Me, e)
    End Sub

#End Region

End Class
